#
#		Author: Alexandros Sigalas
#
#      This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 2 of the License, or
#      (at your option) any later version.
#      
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#      
#      You should have received a copy of the GNU General Public License
#      along with this program; if not, write to the Free Software
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#      MA 02110-1301, USA.

from Material import *

def get_objects_and_duplis(oblist, matrices=False):
    '''
    Return a list of real objects and duplicates and optionally their matrices
    oblist: List of Blender Objects
    MATRICES: Boolean - Check to also get the objects matrices default=False
    Returns: List of objects or
             List of tuples of the form:(ob,matrix) if MATRICES is set to True
    NOTE: There is an ugly hack here that excludes all objects whose name
    starts with "dpl_" to exclude objects that are parented to a duplicating
    object, User must name objects properly if hack is used.
    '''
    supported_types = set(("Mesh","Surf","Curve","Text","Meta"))
    result = list()
    for ob in oblist:
        if not ob.type in supported_types:
            continue
        if dupTest(ob):
            for dup_ob, dup_mx in ob.DupObjects:
                if matrices:
                    result.append((dup_ob, dup_mx))
                else:
                    result.append(dup_ob)
        else:
            if matrices:
                mx = ob.mat.copy()
                result.append((ob, mx))
            else:
                result.append(ob)
    return result

def fix_mesh(me, proxy):
    for f in me.faces:
        f.sel = 1
    proxy.link(me)
    me.quadToTriangle(0)
    if prefs["rec_normals"]:
        me.recalcNormals(0)

########## Material parsing ###############################
#-------------------------------------------------
# getMaterials(obj)
# helper function to get the material list of an object in respect of obj.colbits
# (ripped off luxblend)
#-------------------------------------------------

def get_materials(ob, compress=False):
    mats = [None] * 16
    colbits = obj.colbits
    objMats = obj.getMaterials(1)
    data = obj.getData(mesh=1)
    try:
        dataMats = data.materials
    except:
        try:
            dataMats = data.getMaterials(1)
        except:
            dataMats = []
            colbits = 0xffff
    m = max(len(objMats), len(dataMats))
    if m > 0:
        objMats.extend([None]*16)
        dataMats.extend([None]*16)
        for i in range(m):
            if (colbits & (1<<i) > 0):
                mats[i] = objMats[i]
            else:
                mats[i] = dataMats[i]
        if compress:
            mats = [m for m in mats if m]
    else:
        #print "Warning: object %s has no material assigned"%(obj.getName())
        mats = []
    ## clay option
    #if luxProp(Scene.GetCurrent(), "clay", "false").get()=="true":
        #if clayMat==None: clayMat = Material.New("lux_clayMat")
        #for i in range(len(mats)):
            #if mats[i]:
                #mattype = luxProp(mats[i], "type", "").get()
                #if (mattype not in ["portal","light","boundvolume"]): mats[i] = clayMat
    return mats
    
def build_materials_table(oblist):
    materials = dict()
    for ob in oblist:
        tmp = get_blender_materials(ob,True)
        if not tmp:
            #object has no materials
            if not "default" in materials:
                materials["default"] = default_material
            
        for mat in tmp:
            mname = mat.getName()
            if mname not in materials:
                materials[mname] = convertMat(mat)
    if not "default-section" in materials:
        materials["default-section"] = default_section
    return materials


